import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy


class AppTestCaseImplement:
    url = 'http://127.0.0.1:4723/wd/hub'
    param = {
        "deviceName": "8HJ76H49KJQKM7IF",
        "platformName": "Android",
        "platformVersion": "13",
        "appPackage": "com.sina.weibo",
        "appActivity": "com.sina.weibo.SplashActivity"
    }

    def apptestcase1(self):
        time.sleep(2)
        driver = webdriver.Remote(self.url, self.param)
        el1 = driver.find_element(MobileBy.XPATH,
            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.Button[1]")
        el1.click()
        time.sleep(2)
        el2 = driver.find_element(MobileBy.XPATH,
            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.TextView[3]")
        el2.click()
        time.sleep(2)
        el3 = driver.find_element(MobileBy.ACCESSIBILITY_ID, "发现")
        el3.click()
        time.sleep(2)
        el4 = driver.find_element(MobileBy.XPATH,
            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.ImageView")
        el4.click()
        time.sleep(2)
        el5 = driver.find_element(MobileBy.ID, "com.sina.weibo:id/marqueeview")
        el5.click()
        time.sleep(2)
        el6 = driver.find_element(MobileBy.ID, "com.sina.weibo:id/tv_search_keyword")
        el6.send_keys("张大仙")
        time.sleep(2)
        el7 = driver.find_element(MobileBy.XPATH,
            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.widget.RelativeLayout[2]/android.widget.ListView/android.widget.RelativeLayout[4]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ImageView[1]")
        el7.click()
        time.sleep(2)
        # 获取屏幕宽度
        width = driver.get_window_size()['width']
        # 获取屏幕高度
        height = driver.get_window_size()['height']
        for i in range(5):
            # 利用 "swipe" 实现滑动, 屏幕从下向上滑动
            driver.swipe(width * 0.5, height * 0.9, width * 0.5, height * 0.1, 1000)
            time.sleep(2)
        driver.quit()


apptestcase = AppTestCaseImplement()
apptestcase.apptestcase1()
